home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / pvrgjpeg / chendct.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  9KB  |  378 lines

  1. /*************************************************************
  2. Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
  3. PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
  4. Group. If you use this software, you agree to the following: This
  5. program package is purely experimental, and is licensed "as is".
  6. Permission is granted to use, modify, and distribute this program
  7. without charge for any purpose, provided this license/ disclaimer
  8. notice appears in the copies.  No warranty or maintenance is given,
  9. either expressed or implied.  In no event shall the author(s) be
  10. liable to you or a third party for any special, incidental,
  11. consequential, or other damages, arising out of the use or inability
  12. to use the program for any purpose (or the loss of data), even if we
  13. have been advised of such possibilities.  Any public reference or
  14. advertisement of this source code should refer to it as the Portable
  15. Video Research Group (PVRG) code, and not by any author(s) (or
  16. Stanford University) name.
  17. *************************************************************/
  18. /*
  19. ************************************************************
  20. chendct.c
  21.  
  22. A simple DCT algorithm that seems to have fairly nice arithmetic
  23. properties.
  24.  
  25. W. H. Chen, C. H. Smith and S. C. Fralick "A fast computational
  26. algorithm for the discrete cosine transform," IEEE Trans. Commun.,
  27. vol. COM-25, pp. 1004-1009, Sept 1977.
  28.  
  29. ************************************************************
  30. */
  31.  
  32. /*LABEL chendct.c */
  33.  
  34. /*PUBLIC*/
  35.  
  36. extern void ChenDct();
  37. extern void ChenIDct();
  38.  
  39. /*PRIVATE*/
  40.  
  41. /* Standard Macros */
  42.  
  43. #define NO_MULTIPLY
  44.  
  45. #ifdef NO_MULTIPLY
  46. #define LS(r,s) ((r) << (s))
  47. #define RS(r,s) ((r) >> (s))       /* Caution with rounding... */
  48. #else
  49. #define LS(r,s) ((r) * (1 << (s)))
  50. #define RS(r,s) ((r) / (1 << (s))) /* Correct rounding */
  51. #endif
  52.  
  53. #define MSCALE(expr)  RS((expr),9)
  54.  
  55. /* Cos constants */
  56.  
  57. #define c1d4 362L
  58.  
  59. #define c1d8 473L
  60. #define c3d8 196L
  61.  
  62. #define c1d16 502L
  63. #define c3d16 426L
  64. #define c5d16 284L
  65. #define c7d16 100L
  66.  
  67. /*
  68.   VECTOR_DEFINITION makes the temporary variables vectors.
  69.   Useful for machines with small register spaces.
  70.  
  71.   */
  72.  
  73. #ifdef VECTOR_DEFINITION
  74. #define a0 a[0]
  75. #define a1 a[1]
  76. #define a2 a[2]
  77. #define a3 a[3]
  78. #define b0 b[0]
  79. #define b1 b[1]
  80. #define b2 b[2]
  81. #define b3 b[3]
  82. #define c0 c[0]
  83. #define c1 c[1]
  84. #define c2 c[2]
  85. #define c3 c[3]
  86. #endif
  87.  
  88. /*START*/
  89. /*BFUNC
  90.  
  91. ChenDCT() implements the Chen forward dct. Note that there are two
  92. input vectors that represent x=input, and y=output, and must be
  93. defined (and storage allocated) before this routine is called.
  94.  
  95. EFUNC*/
  96.  
  97. void ChenDct(x,y)
  98.      int *x;
  99.      int *y;
  100. {
  101.   register int i;
  102.   register int *aptr,*bptr;
  103. #ifdef VECTOR_DEFINITION
  104.   register int a[4];
  105.   register int b[4];
  106.   register int c[4];
  107. #else  
  108.   register int a0,a1,a2,a3;
  109.   register int b0,b1,b2,b3;
  110.   register int c0,c1,c2,c3;
  111. #endif
  112.  
  113.   /* Loop over columns */
  114.  
  115.   for(i=0;i<8;i++)
  116.     {
  117.       aptr = x+i;
  118.       bptr = aptr+56;
  119.  
  120.       a0 = LS((*aptr+*bptr),2);
  121.       c3 = LS((*aptr-*bptr),2);
  122.       aptr += 8;
  123.       bptr -= 8;
  124.       a1 = LS((*aptr+*bptr),2);
  125.       c2 = LS((*aptr-*bptr),2);
  126.       aptr += 8;
  127.       bptr -= 8;
  128.       a2 = LS((*aptr+*bptr),2);
  129.       c1 = LS((*aptr-*bptr),2);
  130.       aptr += 8;
  131.       bptr -= 8;
  132.       a3 = LS((*aptr+*bptr),2);
  133.       c0 = LS((*aptr-*bptr),2);
  134.       
  135.       b0 = a0+a3;
  136.       b1 = a1+a2;
  137.       b2 = a1-a2;
  138.       b3 = a0-a3;
  139.  
  140.       aptr = y+i;
  141.  
  142.       *aptr = MSCALE(c1d4*(b0+b1));
  143.       aptr[32] = MSCALE(c1d4*(b0-b1));
  144.  
  145.       aptr[16] = MSCALE((c3d8*b2)+(c1d8*b3));
  146.       aptr[48] = MSCALE((c3d8*b3)-(c1d8*b2));
  147.  
  148.       b0 = MSCALE(c1d4*(c2-c1));
  149.       b1 = MSCALE(c1d4*(c2+c1));
  150.  
  151.       a0 = c0+b0;
  152.       a1 = c0-b0;
  153.       a2 = c3-b1;
  154.       a3 = c3+b1;
  155.  
  156.       aptr[8] = MSCALE((c7d16*a0)+(c1d16*a3));
  157.       aptr[24] = MSCALE((c3d16*a2)-(c5d16*a1));
  158.       aptr[40] = MSCALE((c3d16*a1)+(c5d16*a2));
  159.       aptr[56] = MSCALE((c7d16*a3)-(c1d16*a0));
  160.     }
  161.   
  162.   for(i=0;i<8;i++)
  163.     {       /* Loop over rows */
  164.       aptr = y+LS(i,3);
  165.       bptr = aptr+7;
  166.  
  167.       c3 = RS((*(aptr)-*(bptr)),1);
  168.       a0 = RS((*(aptr++)+*(bptr--)),1);
  169.       c2 = RS((*(aptr)-*(bptr)),1);
  170.       a1 = RS((*(aptr++)+*(bptr--)),1);
  171.       c1 = RS((*(aptr)-*(bptr)),1);
  172.       a2 = RS((*(aptr++)+*(bptr--)),1);
  173.       c0 = RS((*(aptr)-*(bptr)),1);
  174.       a3 = RS((*(aptr)+*(bptr)),1);
  175.  
  176.       b0 = a0+a3;
  177.       b1 = a1+a2;
  178.       b2 = a1-a2;
  179.       b3 = a0-a3;
  180.  
  181.       aptr = y+LS(i,3);
  182.       
  183.       *aptr = MSCALE(c1d4*(b0+b1));
  184.       aptr[4] = MSCALE(c1d4*(b0-b1));
  185.       aptr[2] = MSCALE((c3d8*b2)+(c1d8*b3));
  186.       aptr[6] = MSCALE((c3d8*b3)-(c1d8*b2));
  187.  
  188.       b0 = MSCALE(c1d4*(c2-c1));
  189.       b1 = MSCALE(c1d4*(c2+c1));
  190.  
  191.       a0 = c0+b0;
  192.       a1 = c0-b0;
  193.       a2 = c3-b1;
  194.       a3 = c3+b1;
  195.  
  196.       aptr[1] = MSCALE((c7d16*a0)+(c1d16*a3));
  197.       aptr[3] = MSCALE((c3d16*a2)-(c5d16*a1));
  198.       aptr[5] = MSCALE((c3d16*a1)+(c5d16*a2));
  199.       aptr[7] = MSCALE((c7d16*a3)-(c1d16*a0));
  200.     }
  201.  
  202.   /* We have an additional factor of 8 in the Chen algorithm. */
  203.  
  204.   for(i=0,aptr=y;i<64;i++,aptr++)
  205.     *aptr = (((*aptr<0) ? (*aptr-4) : (*aptr+4))/8);
  206. }
  207.  
  208.  
  209. /*BFUNC
  210.  
  211. ChenIDCT() implements the Chen inverse dct. Note that there are two
  212. input vectors that represent x=input, and y=output, and must be
  213. defined (and storage allocated) before this routine is called.
  214.  
  215. EFUNC*/
  216.  
  217. void ChenIDct(x,y)
  218.      int *x;
  219.      int *y;
  220. {
  221.   register int i;
  222.   register int *aptr;
  223. #ifdef VECTOR_DEFINITION
  224.   register int a[4];
  225.   register int b[4];
  226.   register int c[4];
  227. #else  
  228.   register int a0,a1,a2,a3;
  229.   register int b0,b1,b2,b3;
  230.   register int c0,c1,c2,c3;
  231. #endif
  232.  
  233.   /* Loop over columns */
  234.  
  235.   for(i=0;i<8;i++)
  236.     {
  237.       aptr = x+i;
  238.       b0 = LS(*aptr,2);
  239.       aptr += 8;
  240.       a0 = LS(*aptr,2);
  241.       aptr += 8;
  242.       b2 = LS(*aptr,2);
  243.       aptr += 8;
  244.       a1 = LS(*aptr,2);
  245.       aptr += 8;
  246.       b1 = LS(*aptr,2);
  247.       aptr += 8;
  248.       a2 = LS(*aptr,2);
  249.       aptr += 8;
  250.       b3 = LS(*aptr,2);
  251.       aptr += 8;
  252.       a3 = LS(*aptr,2);
  253.       
  254.       /* Split into even mode  b0 = x0  b1 = x4  b2 = x2  b3 = x6.
  255.      And the odd terms a0 = x1 a1 = x3 a2 = x5 a3 = x7.
  256.      */
  257.  
  258.       c0 = MSCALE((c7d16*a0)-(c1d16*a3));
  259.       c1 = MSCALE((c3d16*a2)-(c5d16*a1));
  260.       c2 = MSCALE((c3d16*a1)+(c5d16*a2));
  261.       c3 = MSCALE((c1d16*a0)+(c7d16*a3));
  262.  
  263.       /* First Butterfly on even terms.*/
  264.  
  265.       a0 = MSCALE(c1d4*(b0+b1));
  266.       a1 = MSCALE(c1d4*(b0-b1));
  267.  
  268.       a2 = MSCALE((c3d8*b2)-(c1d8*b3));
  269.       a3 = MSCALE((c1d8*b2)+(c3d8*b3));
  270.  
  271.       b0 = a0+a3;
  272.       b1 = a1+a2;
  273.       b2 = a1-a2;
  274.       b3 = a0-a3;
  275.  
  276.       /* Second Butterfly */
  277.  
  278.       a0 = c0+c1;
  279.       a1 = c0-c1;
  280.       a2 = c3-c2;
  281.       a3 = c3+c2;
  282.  
  283.       c0 = a0;
  284.       c1 = MSCALE(c1d4*(a2-a1));
  285.       c2 = MSCALE(c1d4*(a2+a1));
  286.       c3 = a3;
  287.       
  288.       aptr = y+i;
  289.       *aptr = b0+c3;
  290.       aptr += 8;
  291.       *aptr = b1+c2;
  292.       aptr += 8;
  293.       *aptr = b2+c1;
  294.       aptr += 8;
  295.       *aptr = b3+c0;
  296.       aptr += 8;
  297.       *aptr = b3-c0;
  298.       aptr += 8;
  299.       *aptr = b2-c1;
  300.       aptr += 8;
  301.       *aptr = b1-c2;
  302.       aptr += 8;
  303.       *aptr = b0-c3;
  304.     }
  305.  
  306.   /* Loop over rows */  
  307.  
  308.   for(i=0;i<8;i++)
  309.     {
  310.       aptr = y+LS(i,3);
  311.       b0 = *(aptr++);
  312.       a0 = *(aptr++);
  313.       b2 = *(aptr++);
  314.       a1 = *(aptr++);
  315.       b1 = *(aptr++);
  316.       a2 = *(aptr++);
  317.       b3 = *(aptr++);
  318.       a3 = *(aptr);
  319.       
  320.       /*
  321.     Split into even mode  b0 = x0  b1 = x4  b2 = x2  b3 = x6.
  322.     And the odd terms a0 = x1 a1 = x3 a2 = x5 a3 = x7.
  323.     */
  324.  
  325.       c0 = MSCALE((c7d16*a0)-(c1d16*a3));
  326.       c1 = MSCALE((c3d16*a2)-(c5d16*a1));
  327.       c2 = MSCALE((c3d16*a1)+(c5d16*a2));
  328.       c3 = MSCALE((c1d16*a0)+(c7d16*a3));
  329.  
  330.       /* First Butterfly on even terms.*/
  331.  
  332.       a0 = MSCALE(c1d4*(b0+b1));
  333.       a1 = MSCALE(c1d4*(b0-b1));
  334.  
  335.       a2 = MSCALE((c3d8*b2)-(c1d8*b3));
  336.       a3 = MSCALE((c1d8*b2)+(c3d8*b3));
  337.  
  338.       /* Calculate last set of b's */
  339.  
  340.       b0 = a0+a3;
  341.       b1 = a1+a2;
  342.       b2 = a1-a2;
  343.       b3 = a0-a3;
  344.  
  345.       /* Second Butterfly */
  346.  
  347.       a0 = c0+c1;
  348.       a1 = c0-c1;
  349.       a2 = c3-c2;
  350.       a3 = c3+c2;
  351.  
  352.       c0 = a0;
  353.       c1 = MSCALE(c1d4*(a2-a1));
  354.       c2 = MSCALE(c1d4*(a2+a1));
  355.       c3 = a3;
  356.       
  357.       aptr = y+LS(i,3);
  358.       *(aptr++) = b0+c3;
  359.       *(aptr++) = b1+c2;
  360.       *(aptr++) = b2+c1;
  361.       *(aptr++) = b3+c0;
  362.       *(aptr++) = b3-c0;
  363.       *(aptr++) = b2-c1;
  364.       *(aptr++) = b1-c2;
  365.       *(aptr) = b0-c3;
  366.     }
  367.   
  368.   /*
  369.     Retrieve correct accuracy. We have additional factor
  370.     of 16 that must be removed.
  371.    */
  372.  
  373.   for(i=0,aptr=y;i<64;i++,aptr++)
  374.     *aptr = (((*aptr<0) ? (*aptr-8) : (*aptr+8)) /16);
  375. }
  376.  
  377. /*END*/
  378.